<?php

class Tourism_provider_category_model extends CI_Model
{
    private $table = 'tourism_provider_category';

     /**
     * 根据uid 获取
     * @param unknown $uid
     */
    public function providerCategoryByUid($uid)
    {
        $this->db->where('provider_uid', $uid);
        $this->db->where('is_on_sale', 1);  // 上架通过
        return $this->db->get($this->table);
    }

     /**
     * 总数
     * @param unknown $getData
     */
    public function total($getData)
    {

        $this->db->join('user', 'user.uid=tourism_provider_category.provider_uid');
        if ( !empty($getData['uid']) ) {
            $this->db->where('tourism_provider_category.provider_uid', $getData['uid']);
        }
        if ( !empty($getData['is_on_sale']) ) {
            $this->db->where('tourism_provider_category.is_on_sale', $getData['is_on_sale']);
        }

        return $this->db->count_all_results($this->table);
    }

    /**
     * 列表
     * @param unknown $page_num
     * @param unknown $num
     * @param unknown $getData
     */
    public function page_list($page_num, $num, $getData)
    {

        $this->db->select('tourism_provider_category.*,user.user_name');
        $this->db->from($this->table);
        $this->db->join('user', 'user.uid=tourism_provider_category.provider_uid');
        if ( !empty($getData['uid']) ) {
            $this->db->where('tourism_provider_category.provider_uid', $getData['uid']);
        }
        if ( !empty($getData['is_on_sale']) ) {
            $this->db->where('tourism_provider_category.is_on_sale', $getData['is_on_sale']);
        }
        $this->db->order_by('creat_at', 'desc');
        $this->db->limit($page_num, $num);

        return $this->db->get();
    }

    /**
     * 状态位
     * @param unknown $flag
     * @param unknown $id
     */
    public function updateTourismProviderCategoryFlag($flag, $id)
    {
        $data['is_on_sale'] = $flag;
        if (is_array($id)) {
            $this->db->where_in('id',$id);
        } else {
        	$this->db->where('id',$id);
        }
        return $this->db->update($this->table,$data);
    }

    public function delete($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }

    /**
     * @descripte   insert
     * @Author xiumao
     * @date 2016/6/8 0008 上午 9:33
     */
    public function insertProviderCategory($params)
    {
        $data = array(
            'provider_uid'  => $params['provider_uid'],
            'category_name' => $params['category_name'],
            'is_main'       => isset($params['is_main']) ? $params['is_main'] : '1',
            'is_on_sale'    => isset($params['is_on_sale']) ? $params['is_on_sale'] : '2',
            'sort'          => isset($params['sort']) ? $params['sort'] : '1',
            'creat_at'      => date('Y-m-d H:i:s'),
        );

        return $this->db->insert($this->table, $data);
    }

    /**
     * @descripte   验证同一供应商 类别是否重复
     * @Author xiumao
     * @date 2016/6/8 0008 上午 9:44
     */
    public function checkUidCategoryName($provider_uid, $category_name, $id = false)
    {
        $this->db->from($this->table);
        $this->db->where('provider_uid', $provider_uid);
        $this->db->where('category_name', $category_name);
        if ( !empty($id) ) {
            $this->db->where('id !=', $id);
        }

        return $this->db->get();
    }

    /**
     * @descripte   设置主推类别
     * @Author xiumao
     * @date 2016/6/8 0008 上午 10:06
     */
    public function setProviderCategoryMain($params, $id)
    {
        $data['is_main'] = '1';
        $this->db->where('provider_uid', $params['provider_uid']);
        $this->db->where('id != ', $id);


        return $this->db->update($this->table, $data);
    }

    /**
     * @descripte   查找
     * @Author xiumao
     * @date 2016/6/8 0008 上午 11:07
     */
    public function findById($id)
    {
        $this->db->where('id', $id);

        return $this->db->get($this->table);
    }

    /**
     * @descripte   update
     * @Author xiumao
     * @date 2016/6/8 0008 上午 11:24
     */
    public function updateProviderCategory($params)
    {
        $data = array(
            'provider_uid'  => $params['provider_uid'],
            'category_name' => $params['category_name'],
            'is_main'       => isset($params['is_main']) ? $params['is_main'] : '1',
            'is_on_sale'    => isset($params['is_on_sale']) ? $params['is_on_sale'] : '2',
            'sort'          => isset($params['sort']) ? $params['sort'] : '1',
            'creat_at'      => date('Y-m-d H:i:s'),
        );

        $this->db->where('id', $params['id']);
        return $this->db->update($this->table, $data);
    }
}